import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class DeletFileServlet extends HttpServlet {
    
    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
        throws IOException, ServletException
    {
        response.setContentType("text/html");

        PrintWriter out = response.getWriter();

        String file_dir = request.getParameter("dir");
	String file_id = request.getParameter("id");
        if (file_id != null) {
            out.println(" file id = " + file_id);

	    File fileName = new File (file_dir, file_id);
	   
	    if (fileName.exists()) {
		System.out.println("before deleted file: "+fileName.getName());
		if(fileName.delete()) {
		    System.out.println("file "+ fileName.getName()+" has been deleted.");

		    //request.setAttribute("selectedScreen", request.getServletPath());
		    request.setAttribute("selectedScreen", "http://192.168.1.101");
		    System.out.println(request.getAttribute("selectedScreen"));

		    RequestDispatcher dispatcher = request.getRequestDispatcher("Apache2");	
		    if (dispatcher != null) {
			dispatcher.forward(request, response);	
		    }
		    else {
			out.println("dispatcher == null");
		    }
		}
	    } else {
		out.println("<BR>No such file");
		out.println("<BR>file path = "+fileName.getPath());
		out.println("<BR>file name = "+fileName.getName());
	    }
	}
    }

    public void doPost(HttpServletRequest request,
                      HttpServletResponse response)
        throws IOException, ServletException
    {
        doGet(request, response);
    }

}
